#include "game.h"

#include "../client/client.h"

void g_decollide_player_map(player_t * p, int stepx, int stepy)
{
	if (p->x < 0) {
		p->x = 0;
	} else if ((p->x + p->w) > rd.map.w) {
		p->x = rd.map.w - p->w;
	}

	if (p->y < 0) {
		p->y = 0;
	} else if ((p->y + p->h) > rd.map.h) {
		p->y = rd.map.h - p->h;
	}

	rect_t tile = {.w = rd.map.tw, .h = rd.map.th};
	rect_t player = {.w = p->w, .h = p->h,
					 .x = p->x, .y = p->y};
	int i, j;
	for (i = 0; i < rd.map.trows; i++) {
	for (j = 0; j < rd.map.tcols; j++) {
		if (rd.map.tile[rd.map.tcols*i+j] > 1) {//碰撞物体代号，待定
			tile.x = j * rd.map.tw;
			tile.y = i * rd.map.th;

			if (rd_rect_collision(&player, &tile) == true) {
				if (p->x > tile.x) {
					if (p->x < tile.x + tile.w) 
						p->x -= stepx;
				} else {
					if (tile.x < p->x + p->w)
						p->x -= stepx;
				}

				if (p->y > tile.y) {
					if (p->y < tile.y + tile.h)
						p->y -= stepy;
				} else {
					if (tile.y < p->y + p->h)
						p->y -= stepy;
				}

				cl_debug("0123456789"+(i%9));
				return;
			}
		}
	}}
}
